package com.wyd.wenyongdaAdmin.admin.dict.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.wyd.wenyongdaAdmin.commons.model.admin.dict.dto.DictDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.dict.vo.DictVO;
import com.wyd.wenyongdaAdmin.commons.util.ResultEnum;
import com.wyd.wenyongdaAdmin.commons.util.ResultT;
import com.wyd.wenyongdaAdmin.commons.util.ResultUtil;
import com.wyd.wenyongdaAdmin.xo.admin.dict.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "字典控制器")
@RestController
@RequestMapping("/dict")
public class DictController {
    private static final Logger log = LoggerFactory.getLogger(DictController.class);

    private final IDictService dictService;

    @Autowired
    DictController (IDictService dictService) {
        this.dictService = dictService;
    }

    @ApiOperation("分页查询数据字典")
    @PostMapping("/get/page")
    public ResultUtil selectDictPageWithCount(@RequestBody Map<String, Object> reqParams) {
        DictDTO dictDTO = null;
        if (ObjectUtil.isNotNull(reqParams.get("name")) || ObjectUtil.isNotNull(reqParams.get("type"))) {
            dictDTO = new DictDTO();
            if (ObjectUtil.isNotNull(reqParams.get("name"))) {
                dictDTO.setName(reqParams.get("name").toString());
            }
            if (ObjectUtil.isNotNull(reqParams.get("type"))) {
                dictDTO.setType(reqParams.get("type").toString());
            }
        }
        Integer pageNumber = (Integer) reqParams.get("pageNumber");
        Integer pageSize = (Integer) reqParams.get("pageSize");
        PageInfo<DictDTO> pageInfo = this.dictService.selectPageWithCount(pageNumber, pageSize, dictDTO);
        return ResultUtil.ok().data("records", pageInfo);
    }

    @PostMapping("/save")
    public ResultUtil addDict(@RequestBody Map<String, Object> reqParams) {
        DictDTO dictDTO = BeanUtil.fillBeanWithMap(reqParams, new DictDTO(), false);
        if (this.dictService.addDict(dictDTO) == 0) {
            return ResultUtil.error(ResultEnum.DICT_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @PutMapping("/update")
    public ResultUtil updateDict(@RequestBody Map<String, Object> reqParams) {
        DictDTO dictDTO = BeanUtil.fillBeanWithMap(reqParams, new DictDTO(), false);
        if (this.dictService.updateDict(dictDTO) == 0) {
            return ResultUtil.error(ResultEnum.DICT_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteDict(@RequestBody Map<String, Object> reqParams) {
        if (ObjectUtil.isNotNull(reqParams.get("ids"))) {
            List<Long> ids = Convert.toList(Long.TYPE ,reqParams.get("ids"));
            if (this.dictService.deleteDict(ids) == 0) {
                return ResultUtil.error(ResultEnum.DICT_ERROR_OR_UNKNOWN);
            } else {
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
    }

    @GetMapping("/get/dict/with/entry")
    public ResultT<List<DictVO>> getDictWithEntry() {
        List<DictVO> dictWithDictEntry = this.dictService.getDictWithDictEntry();
        if (dictWithDictEntry.size() > 0) {
            return ResultT.<List<DictVO>>ok().data(dictWithDictEntry);
        } else {
            return ResultT.error(ResultEnum.DICT_ERROR_OR_UNKNOWN);
        }
    }
}
